//
//  WMH_User_ChangePassword_ViewController.m
//  wmh.ios.v5
//
//  Created by AndLi on 14-8-29.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//  密码修改

#import "WMH_User_ChangePassword_ViewController.h"

@interface WMH_User_ChangePassword_ViewController ()

@end

@implementation WMH_User_ChangePassword_ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"密码修改";
    NSString *loginid = [DBFrameService getConfigValue:Epoint_User_LoginId];
    [self.loginid_TextField setText:loginid];
    
    [self.view setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    
}

//键盘退出
-(void) returnjianpan{
    [self.loginid_TextField resignFirstResponder];
    [self.oldpwd_TextField resignFirstResponder];
    [self.newpwd_TextField resignFirstResponder];
    [self.newpwd2_TextField resignFirstResponder];
}

-(void)changePwd:(id)sender{
    [self returnjianpan];
    NSString *oldpassword = [self.oldpwd_TextField text];
    NSString *newpassword = [self.newpwd_TextField text];
    
    if([self checkDate]){
        
        RIButtonItem *okItem = [RIButtonItem item];
        okItem.label = @"确定";
        okItem.action = ^
        {
            NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
            [params setObject:[DBFrameService getConfigValue:Epoint_User_LoginId] forKey:@"loginid"];
            [params setObject:[MDUtil authPassword:oldpassword] forKey:@"oldpassword"];
            [params setObject:[MDUtil authPassword:newpassword] forKey:@"newpassword"];
            [self startTask:@"Task_changePwb" withParams:params withTaskId:1];
        };
        
        RIButtonItem *noItem = [RIButtonItem item];
        noItem.label = @"取消";
        
        //调用
        NSString *mess = [NSString stringWithFormat:@"确定要修改密码吗？"];
        
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:mess cancelButtonItem:noItem otherButtonItems:okItem, nil];
        [alter show];
        
    }
    
}

-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj{
    //处理异常信息
    NSMutableArray *arr = (NSMutableArray*)obj;
    if (![[arr objectAtIndex:0]isEqualToString:@"1"]) {
        [self toastMessage:[arr objectAtIndex:1]];
        return;
    }
    
    if (taskId==1) {
        NSString *bs= [arr objectAtIndex:2];
        if([bs rangeOfString:@"True"].location!=NSNotFound){
            
            
            [self toastMessage:@"修改成功!"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self toastMessage:@"修改失败!"];
        }
    }
    
}

-(void)textFiledReturnEditing:(id)sender{
    [sender resignFirstResponder];
}


//数据校验
-(BOOL) checkDate{
    
    NSString *oldpassword = [self.oldpwd_TextField text];
    NSString *newpassword = [self.newpwd_TextField text];
    NSString *newpassword2 = [self.newpwd2_TextField text];


    if([oldpassword isEqualToString:@""]){
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"原密码不能为空！"];
        return false;
    }
    else if([newpassword isEqualToString:@""]){
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"新密码不能为空！"];
        return false;
    }
    else if ([newpassword length] <= 5) {
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"密码长度小于6！"];
        return false;
    }
    else if([newpassword2 isEqualToString:@""]){
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"请再次输入新密码！"];
        return false;
    }
    
    
    if(![newpassword isEqualToString:newpassword2]){
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"2次密码输入不一致！"];
        return false;

    }
    return true;
    
}
-(IBAction)checkPassWordLength:(id)sender{
    if (self.newpwd_TextField.text.length>16) {
        self.newpwd_TextField.text = [self.newpwd_TextField.text substringToIndex:16];
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"密码长度不能大于16！"];
    }
}

@end
